<?php

require('config.php');
$vkontakteAccessToken = file_get_contents('token.txt');

function vk_method($method, $params) {
    $vkontakteAccessToken = file_get_contents('token.txt');
    $url = "https://api.vkontakte.ru/method/".$method."?access_token=".$vkontakteAccessToken."&";
    foreach($params as $key => $value) {
        $url .= $key."=".urlencode($value)."&";
    };
    echo "<p>Requesting $url</p>";
    $response = file_get_contents($url);
    $oResponse = json_decode($response);
    return $response;
}

function vk_post($text, $user_id) {
    require 'config.php';
    $params = array(
        'owner_id' => $user_id,
        'message' => $text
        );
    $response = vk_method('wall.post', $params);
    echo $response."\n";
    return json_decode($response);
};

function get_user_settings() {
    $response = vk_method('getUserSettings');
    echo $response."\n";
    $oResponse = json_decode($response);
};

function friends() {
    $response = vk_method('friends.get', array('count'=>10));
    echo $response."<br>";
};


if (isset($_GET['post'])) {
    $text = $_GET['post'];
    if (!$text) $text = 'test!';
    vk_post($text, $vkontakteUserId);
};

if (isset($_GET['get_user_settings'])) {
    get_user_settings();
};

if (isset($_GET['friends'])) {
    friends();
};
if (isset($_GET['days'])) {
    post_days();
};
?>
<h1>Методы</h1>
<a href="/vkpost.php?post">wall.post</a><br>
<a href="/vkpost.php?friends">friends.get</a><br>
<a href="/vkpost.php?get_user_settings">getUserSettings</a><br>
<br>
<?php
    $link = "http://api.vkontakte.ru/oauth/authorize?client_id=".$vkontakteApplicationId.
        "&scope=".urlencode('wall,friends').
        "&redirect_uri=http://api.vkontakte.ru/blank.html".
        "&response_type=code";
    echo ("<p><a href='$link'>Авторизация Вконтакте</a></p>");
?>